/***************************************************************************
 *   Copyright (C) 2008 by Branan Riley   *
 *   branan@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "mainwindow.h"

#include "filelist.h"
#include "renderview.h"
#include "fs_support.h"
#include "gamedialog.h"

#include <QHBoxLayout>
#include <QTimer>
#include <QStatusBar>
#include <QStringList>

MainWindow::MainWindow(QWidget *parent)
 : QMainWindow(parent)
{
  _list = new FileList;
  _view = new RenderView;

  _layout = new QHBoxLayout;
  _layout->addWidget(_list);
  _layout->addWidget(_view);

  _central_widget = new QWidget();
  _central_widget->setLayout(_layout);

  setCentralWidget(_central_widget);

  _dialog = new GameDialog(this);
  _dialog->show();

  statusBar(); // Just calling statusBar() creates a status bar to be used later.

  connect(_list, SIGNAL(selectionChanged(const QString&)), this, SLOT(setModel(const QString&)));
  connect(_list, SIGNAL(selectionChanged(const QString&)), _view, SLOT(setModel(const QString&)));
  
  connect(_dialog, SIGNAL(accepted()), this, SLOT(setupFilesystem()));
  connect(_dialog, SIGNAL(rejected()), qApp, SLOT(quit()));
}


MainWindow::~MainWindow()
{
}

void MainWindow::setupFilesystem()
{
  QStringList list;
  QString basedir = "base";

  if(_dialog->game() == "Quake 4") {
    basedir = "q4base";
  }

  statusBar()->showMessage("Setting up search path...");
  qApp->processEvents();
  SetupEnginePaths(_dialog->gameDir(), basedir, _dialog->fsGame(),
                   _dialog->fsGameBase());

  statusBar()->showMessage("Searching for ASE files...");
  qApp->processEvents();
  list = FindFilesByExtension("ase");
  _list->addFiles(list);

  statusBar()->showMessage("Searching for LWO files...");
  qApp->processEvents();
  list = FindFilesByExtension("lwo");
  _list->addFiles(list);
  statusBar()->clearMessage();
}

void MainWindow::setModel(const QString &filename)
{
  statusBar()->showMessage(filename);
}
